package app

import (
	"fmt"

	"minicommit/internal/routes"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
)

type App struct {
	Name       string
	Version    string
	Port       string
	WorkDir    string
	BotToken   string
	AdminID    int64
	BranchName string
}

func New(port, workdir, vers, name, barnchname string) *App {
	return &App{
		Name:       name,
		Version:    vers,
		Port:       port,
		WorkDir:    workdir,
		BranchName: barnchname,
	}
}

func (a *App) Start() error {

	fbr := fiber.New()
	fbr.Use(cors.New(cors.Config{
		AllowCredentials: true,
	}))

	fbr.Use(func(c *fiber.Ctx) error {
		c.Locals("workDir", a.WorkDir)
		c.Locals("AdminID", a.AdminID)
		c.Locals("BotToken", a.BotToken)
		return c.Next()
	}, cors.New())

	routes.Setup(fbr)
	port := ":" + a.Port
	err := fbr.Listen(port)
	if err != nil {
		fmt.Printf(err.Error())
		return err
	}
	return nil
}
